home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / TUTORC.ZIP / TUT11.C < prev    next >
C/C++ Source or Header  |  1994-10-30  |  9KB  |  275 lines

  1. /* 
  2.   tut11.c
  3.   10/30/94
  4.   from tutprog11.pas
  5.   Adapted from Denthor's tutprog11.pas
  6.   Translated into C, from Denthor's VGA Trainer, by
  7.   Steve Pinault, scp@ohm.att.com
  8.   Compiled with Microsoft Visual C++ 1.5 (Microsoft C 8.0)
  9.   To compile:
  10.   First compile the subroutines in tutsubs.c with the batch file 
  11.   cltutsub.bat
  12.   Then compile any of the tutor programs with the batch file
  13.   cltut.bat
  14.   Example: C:>cltutsub
  15.            C:>cltut tut11.c
  16.            to compile this program.
  17. */
  18.  
  19. #include "tutheadr.h"
  20.  
  21. char source[256][3];
  22. char dest[256][3];
  23. int dir;
  24.  
  25. // {DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD}
  26. // Procedure LoadCELPal (FileName : String; Var Palette : Pallette);
  27. //   { This loads in the pallette of the .CEL file into the variable Palette }
  28. void loadcelpal(char* FileName, char Palette[][3] )
  29. {
  30.   FILE* fptr;
  31.  
  32.   if((fptr=fopen(FileName,"rb"))==NULL)
  33.   {
  34.     printf("Error opening file %s\n",FileName); 
  35.     exit(1);
  36.   }
  37.   fseek(fptr,(long)32,SEEK_SET);
  38.   fread(Palette,1,768,fptr);
  39.   fclose(fptr);
  40. }
  41.  
  42. // {DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD}
  43. // Procedure Init;
  44. void Init()
  45. {
  46.   _fmemset(source,0,768);
  47.   _fmemset(dest,0,768);
  48. }
  49.  
  50. // {DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD}
  51. // Procedure SetItUp;
  52. //   { We define our third screen here }
  53. void SetItUp()
  54. {
  55.     int loop1,loop2,loop3;
  56.     char Pal1[256][3];
  57.     char Pal2[256][3];
  58.     int change;
  59.     char r,g,b,r1,g1,b1;
  60.     int where;
  61.  
  62.     Cls (0,Vaddr2);
  63.  
  64.     for(loop1=0;loop1<256;loop1++) Pal ((char)loop1,0,0,0);
  65.  
  66.   LoadCEL ("to.cel",Virtual2);
  67.   loadcelpal ("to.cel",Pal2);
  68.   Flip2(Vaddr2,VGA);
  69.   LoadCEL ("from.cel",Virtual);
  70.   loadcelpal ("from.cel",Pal1);
  71.  
  72.   where=0;
  73.  
  74.   for(loop1=0;loop1<320;loop1++)
  75.     for(loop2=0;loop2<200;loop2++)
  76.     {
  77.       if((GetPixel(loop1,loop2,Vaddr)!=0) || (GetPixel (loop1,loop2,VGA)!=0))
  78.       {
  79.         change=FALSE;
  80.         r=Pal1[GetPixel(loop1,loop2,Vaddr)][0];
  81.         g=Pal1[GetPixel(loop1,loop2,Vaddr)][1];
  82.         b=Pal1[GetPixel(loop1,loop2,Vaddr)][2];
  83.         r1=Pal2[GetPixel(loop1,loop2,VGA)][0];
  84.         g1=Pal2[GetPixel(loop1,loop2,VGA)][1];
  85.         b1=Pal2[GetPixel(loop1,loop2,VGA)][2];
  86.             
  87.         for(loop3=0;loop3<=where;loop3++)                              
  88.           if ((source[loop3][0]==r) && (source[loop3][1]==g) && (source[loop3][2]==b) &&
  89.              (dest[loop3][0]==r1) && (dest[loop3][1]==g1) && (dest[loop3][2]==b1))
  90.           {    
  91.              PutPixel (loop1,loop2,(char)loop3,Vaddr2);
  92.              change=TRUE;
  93.           }
  94.         //  { Here we check that this combination hasn't occured before. If it
  95.         //    has, put the appropriate pixel onto the third screen (vaddr2) }
  96.  
  97.         if (!(change) )
  98.         {
  99.           where++;
  100.           if (where==256)
  101.           {
  102.             SetText();
  103.             printf("Pictures have too many colors! Squeeze then retry!\n");
  104.             exit(1);
  105.             // { There were too many combinations of colors. Alter picture and
  106.             //  then retry }
  107.             // E.g. 16 colors per picture would be a max of 256 combinations, so
  108.             // should work every time.
  109.           }
  110.           PutPixel(loop1,loop2,(char)where,Vaddr2);
  111.           source[where][0]=Pal1[GetPixel(loop1,loop2,Vaddr)][0];
  112.           source[where][1]=Pal1[GetPixel(loop1,loop2,Vaddr)][1];
  113.           source[where][2]=Pal1[GetPixel(loop1,loop2,Vaddr)][2];
  114.           dest[where][0]=Pal2[GetPixel(loop1,loop2,VGA)][0];
  115.           dest[where][1]=Pal2[GetPixel(loop1,loop2,VGA)][1];
  116.           dest[where][2]=Pal2[GetPixel(loop1,loop2,VGA)][2];
  117.           //  { Create a new color and set it's from and to pallette values }
  118.         }
  119.       }
  120.     }
  121.   Cls (0,VGA);
  122.   Flip2(Vaddr2,VGA);
  123. }
  124.  
  125. // {DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD}
  126. // Procedure Crossfade (direction:boolean;del,farin:word);
  127. // { This fades from one picture to the other in the direction specified
  128. //     with a del delay. It crossfades one degree for every value in farin.
  129. //    If farin=63, then a complete crossfade occurs }
  130. void Crossfade(int direction, int del, int farin)
  131. {
  132.   int loop1,loop2;
  133.   char temp[256][3];
  134.   char far* tempptr=&temp[0][0];
  135.     
  136.   if (direction)
  137.   {
  138.     _fmemcpy(temp,source,768);
  139.     for(loop1=0;loop1<256;loop1++)
  140.       Pal ((char)loop1,source[loop1][0],source[loop1][1],source[loop1][2]);
  141.     for(loop1=0;loop1<=farin;loop1++)
  142.     {
  143.       WaitRetrace();
  144.       SetAllPal(tempptr);
  145.  
  146.       for(loop2=0;loop2<256;loop2++)
  147.       {
  148.         if( temp[loop2][0]<dest[loop2][0])temp[loop2][0]++;
  149.         if( temp[loop2][0]>dest[loop2][0])temp[loop2][0]--;
  150.         if( temp[loop2][1]<dest[loop2][1])temp[loop2][1]++;
  151.         if( temp[loop2][1]>dest[loop2][1])temp[loop2][1]--;
  152.         if( temp[loop2][2]<dest[loop2][2])temp[loop2][2]++;
  153.         if( temp[loop2][2]>dest[loop2][2])temp[loop2][2]--;                 
  154.         //  { Move temp (the current pallette) from source to dest }
  155.       }
  156.       delay (del);
  157.     }
  158.   }
  159.   else 
  160.   {
  161.     _fmemcpy(temp,dest,768);
  162.     for(loop1=0;loop1<256;loop1++)
  163.       Pal ((char)loop1,dest[loop1][0],dest[loop1][1],dest[loop1][2]);
  164.     for(loop1=0;loop1<=farin;loop1++)
  165.     {
  166.       WaitRetrace();
  167.       SetAllPal(tempptr);
  168.  
  169.       for(loop2=0;loop2<256;loop2++)
  170.       {
  171.         if( temp[loop2][0]<source[loop2][0])temp[loop2][0]++;
  172.         if( temp[loop2][0]>source[loop2][0])temp[loop2][0]--;
  173.         if( temp[loop2][1]<source[loop2][1])temp[loop2][1]++;
  174.         if( temp[loop2][1]>source[loop2][1])temp[loop2][1]--;
  175.         if( temp[loop2][2]<source[loop2][2])temp[loop2][2]++;
  176.         if( temp[loop2][2]>source[loop2][2])temp[loop2][2]--;                 
  177.         //  { Move temp (the current pallette) from source to dest }
  178.       }
  179.       delay (del);
  180.     }
  181.   }
  182. }
  183.  
  184. void main()
  185. {                      
  186.   int loop1;
  187.   _clearscreen(_GCLEARSCREEN);
  188.   SetUpVirtual();
  189.   SetMCGA();
  190.   Init();
  191.   SetItUp();
  192.   for(loop1=0;loop1<256;loop1++)
  193.     Pal((char)loop1,source[loop1][0],source[loop1][1],source[loop1][2]);
  194.   Flip2(Vaddr2,VGA);
  195.   delay (3000);
  196.  
  197.   dir=TRUE;
  198.   while(1)
  199.   {
  200.     Crossfade(dir,20,63);
  201.     dir=1-dir; // Toggle
  202.     delay (1000);
  203.     if(_bios_keybrd(_KEYBRD_READY))break;
  204.   }
  205.   getch();
  206.   Crossfade(dir,20,20);
  207.   getch();
  208.   SetText();
  209. }
  210. /*  
  211. BEGIN
  212.   clrscr;
  213.   writeln ('Hello there! This trainer program is on cross fading. What will happen');
  214.   writeln ('is this : The program will load in two .CEL files, FROM.CEL and TO.CEL');
  215.   writeln ('into the virtual screen at vaddr and to the VGA screen. The pallettes');
  216.   writeln ('of these two pictures are loaded into pal1 and pal2. Note that you');
  217.   writeln ('could easily rewrite this to load in other types of files if you do');
  218.   writeln ('not own Autodesk Animator to draw your files (The pictures presented');
  219.   writeln ('here were drawn by Fubar, sqeezed by me ;)). A third screen is then');
  220.   Writeln ('generated into vaddr2 (this takes 5-10 seconds on my 386-40). Note');
  221.   writeln ('that you could dump vaddr2 to disk as a file instead of calculating it');
  222.   writeln ('each time...it would be faster and be half the size of the two pictures.');
  223.   Writeln ('The picture will then crossfade between the two. Hit a key and it will');
  224.   writeln ('crossfade halfway and then exit.');
  225.   writeln;
  226.   writeln ('After one particular comment E-Mailed to me, I thought I should just add');
  227.   writeln ('this : I am not an employee of Autodesk, and they do not pay me to promote');
  228.   writeln ('their product. You have no idea how much I wish they would :)  I recieve');
  229.   writeln ('absolutely _nothing_ for writing the trainer...');
  230.   writeln;
  231.   writeln;
  232.   write ('Hit any key to continue ...');
  233.   readkey;
  234.   randomize;
  235.   setupvirtual;
  236.   setmcga;
  237.   init;
  238.   SetItUp;
  239.   for loop1:=0 to 255 do
  240.     pal (loop1,source[loop1,1],source[loop1,2],source[loop1,3]);
  241.   flip (vaddr2,vga);
  242.   delay (3000);
  243.  
  244.   dir:=TRUE;
  245.   while keypressed do readkey;
  246.   repeat
  247.     crossfade(dir,20,63);
  248.     dir:=not (dir);
  249.     delay (1000);
  250.   until keypressed;
  251.   Readkey;
  252.   crossfade(dir,20,20);
  253.   readkey;
  254.   settext;
  255.   Writeln ('All done. This concludes the eleventh sample program in the ASPHYXIA');
  256.   Writeln ('Training series. You may reach DENTHOR under the names of GRANT');
  257.   Writeln ('SMITH/DENTHOR/ASPHYXIA on the ASPHYXIA BBS. I am also an avid');
  258.   Writeln ('Connectix BBS user, and occasionally read RSAProg. E-mail me at :');
  259.   Writeln ('    smith9@batis.bis.und.ac.za');
  260.   Writeln ('The numbers are available in the main text. You may also write to me at:');
  261.   Writeln ('             Grant Smith');
  262.   Writeln ('             P.O. Box 270');
  263.   Writeln ('             Kloof');
  264.   Writeln ('             3640');
  265.   Writeln ('             Natal');
  266.   Writeln ('             South Africa');
  267.   Writeln ('I hope to hear from you soon!');
  268.   Writeln; Writeln;
  269.   Write   ('Hit any key to exit ...');
  270.   readkey;
  271.   shutdown;
  272.   FreeMem (VirScr2,64000);
  273. END.
  274. */
  275.